<template>
  <div class="index">
    <Header></Header>
    <div class="con">
      <div class="con_top">
        <div class="con_top_btn" @click="openDialog">
          发布任务
        </div>
        <div class="con_right_btn" @click="showLogoutDialog">
          退出
        </div>
      </div>
      <div class="con_box">
        <el-table :data="tableData" stripe style="width: 100%">
          <el-table-column prop="task_id" label="任务ID"></el-table-column>
          <el-table-column prop="issuer" label="发布者"></el-table-column>
          <el-table-column prop="task_user" label="执行者"></el-table-column>
          <el-table-column prop="bonus" label="任务奖励"></el-table-column>
          <el-table-column prop="task_name" label="任务描述"></el-table-column>
          <el-table-column label="进度">
            <template slot-scope="scope">
              {{scope.row.task_status | taskStatus}}
            </template>
          </el-table-column>
          <el-table-column prop="comment" label="任务评价"></el-table-column>
          <el-table-column prop="timestamp" label="发布时间"></el-table-column>
          <el-table-column label="动作">
            <template slot-scope="scope">
              <div class="check_btn" @click="openDialog2(scope.row)">操作</div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="paging">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="pageSize"
          layout="total,  prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </div>
    <div class="mask" v-if="dialogFlag">
      <div class="dialog">
        <div class="dialog_top">
          <div class="dialog_top_title">发布任务</div>
          <div class="dialog_top_right">
            <img @click="closeDialog" src="../assets/images/close_icon.png" alt="">
          </div>
        </div>
        <div class="dialog_con">
          <el-form ref="form" :model="taskData" label-width="1.3rem">
            <el-form-item label="任务描述">
              <el-input v-model="taskData.task_name" placeholder="请填写任务描述"></el-input>
            </el-form-item>
            <el-form-item label="任务奖励">
              <el-input v-model="taskData.bonus" placeholder="请填写任务奖励"></el-input>
            </el-form-item>
            <div class="sure_btn" @click="issue(taskData)">发布</div>
          </el-form>
        </div>
      </div>
    </div>
    <div class="mask" v-if="dialogFlag2">
      <div class="dialog">
        <div class="dialog_top">
          <div class="dialog_top_title">操作</div>
          <div class="dialog_top_right">
            <img @click="closeDialog2" src="../assets/images/close_icon.png" alt="">
          </div>
        </div>
        <div class="dialog_con">
          <el-form :model="statusData" label-width="1rem">
            <el-form-item label="状态">
              <el-select v-model="statusData.task_status" placeholder="请选择任务状态">
                <el-option style="font-size:0.16rem;" label="接受" value=1></el-option>
                <el-option style="font-size:0.16rem;" label="提交" value=2></el-option>
                <el-option style="font-size:0.16rem;" label="确认" value=3></el-option>
                <el-option style="font-size:0.16rem;" label="退回" value=4></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="评价">
              <el-input v-model="statusData.comment" placeholder="请填写任务评价"></el-input>
            </el-form-item>
            <div class="sure_btn" @click="update(statusData)">确定</div>
          </el-form>
        </div>
      </div>
    </div>
    <div class="mask" v-if="exitDialogFlag">
      <div class="dialog">
        <div class="dialog_top">
          <div class="dialog_top_title">退出程序</div>
          <div class="dialog_top_right">
            <img @click="closeExitDialog" src="../assets/images/close_icon.png" alt="">
          </div>
        </div>
        <div class="dialog_con" style="{height : 1rem}">
          <div class="sure_btn" @click="exitApp">确定</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Header from "@/components/Header";
import { tasklist, issue, update, logout } from '@/controller/api'
import { taskStatus, removeStore } from '@/controller/utils'
export default {
  created() {
    this.initData()
  },
  filters: {
    taskStatus(status){
      return taskStatus(status)
    }
  },
  data() {
    return {
      lectureName: '',
      currentPage: 1,
      pageSize: 10,
      total: 0,
      dialogFlag: false,
      dialogFlag2: false,
      exitDialogFlag: false,
      tableData: [],
      taskData: {
        task_name: '',
        task_bonus: ''
      },
      statusData: {
        task_id: '',
        task_status: '',
        comment: ''
      }
    };
  },
  methods: {
    initData(){
      tasklist({page: this.currentPage}).then(res=>{
        // console.log(res)
        if(res.data.code == '0'){
          this.tableData = res.data.data.data
          this.total = res.data.data.total
        }else{
          this.$message.error(res.data.msg)
        }
      })
    },
    issue(obj){
      if(/^\s*$/g.test(obj.task_name)){
        this.$message({
          message: '请填写任务描述',
          type: 'error'
        });
        return
      }
      if(/^\s*$/g.test(obj.bonus)){
        this.$message.error('请填写任务奖励')
        return
      }
      const params = {
        task_name: obj.task_name,
        bonus: parseInt(obj.bonus)
      }
      console.log(params)
      issue(params).then(res=>{
        console.log(res)
        if(res.data.code == '0'){
          this.$message.success('发布成功')
          this.dialogFlag = false
          this.initData()
          this.taskData.task_name = ''
          this.taskData.bonus = ''
        }else{
          this.$message.error(res.data.msg)
        }
      })
    },
    update(obj){
      if(!obj.task_status){
        this.$message.error('请选择任务状态')
      }
      var params
      if(obj.comment){
        params = {
          "task_id": obj.task_id,
          "task_status": parseInt(obj.task_status),
          "comment": obj.comment 
        }
      }else{
        params = {
          "task_id": obj.task_id,
          "task_status": parseInt(obj.task_status)
        }
      }
      // console.log(params)
      update(params).then(res=>{
        // console.log(res)
        if(res.data.code == '0'){
          this.initData()
          this.$message.success('操作成功')
          this.dialogFlag2 = false
          this.statusData.task_status = ''
          this.statusData.comment = ''
        }else{
          this.$message.error(res.data.msg)
        }
      })
    },
    openDialog(){
      this.dialogFlag = true
    },
    closeDialog(){
      this.dialogFlag = false
    },
    openDialog2(res){
      this.dialogFlag2 = true
      this.statusData.task_id = res.task_id
    },
    closeDialog2(){
      this.dialogFlag2 = false
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.currentPage = val
      this.initData()
    },
    showLogoutDialog() {
      this.exitDialogFlag = true;
    },
    closeExitDialog() {
      this.exitDialogFlag = false;
    },
    exitApp() {
      logout().then(
        res => {
          console.log(res);
          if (res.data.code == '0') {
            this.$message('退出成功！');
            this.exitDialogFlag = false;
            this.$router.replace('/login');
            removeStore('username')
          } else {
            this.$message.error('退出失败，请重试！');
          }
        }
      ).catch(
        error => {
          console.log(error);
        }
      );
    }
  },
  components: {
    Header
  }
};
</script>

<style lang="less" scoped>
.index {
  .con {
    width: 100%;
    padding-left: 0.5rem;
    padding-top: 1.2rem;
    padding-right: 0.5rem;
    padding-bottom: 0.2rem;
    background: #f6f8fa;
    min-height: 100vh;
    .con_nav{
      overflow: hidden;
      margin-bottom: 0.2rem;
      .con_nav_back{
        float: left;
        font-size: 0.14rem;
        color:rgba(38,39,41,.4);
        line-height: 0.2rem;
        text-decoration: none;
      }
      img{
        float: left;
        width: 0.14rem;
        height: 0.18rem;
        margin: 0 0.1rem;
      }
      .con_nav_title{
        float: left;
        font-size: 0.14rem;
        color:rgba(38,39,41,.4);
        line-height: 0.2rem;
      }
    }
    .con_top {
      overflow: hidden;
      width: 100%;
      height: 0.6rem;
      .con_top_btn {
        cursor: pointer;
        span {
          font-size: 0.22rem;
          font-weight: bold;
          line-height: 0.6rem;
        }
        float: left;
        width: 1.4rem;
        height: 0.6rem;
        line-height: 0.6rem;
        text-align: center;
        background: #0378ff;
        border-radius: 0.08rem;
        color: #fff;
        font-size: 0.18rem;
      }
      .con_right_btn {
        cursor: pointer;
        span {
          font-size: 0.22rem;
          font-weight: bold;
          line-height: 0.6rem;
        }
        float: right;
        width: 1.4rem;
        height: 0.6rem;
        line-height: 0.6rem;
        text-align: center;
        background: #0378ff;
        border-radius: 0.08rem;
        color: #fff;
        font-size: 0.18rem;
      }
    }
    .con_box {
      margin-top: 0.3rem;
      background-color: #fff;
      width: 100%;
      min-height: 8.8rem;
      /deep/.el-table{
        border-radius: 0.08rem;
        text-align: center;
      }
      /deep/.el-table .cell{
        font-size: 0.14rem;
      }
      /deep/.el-table th, .el-table tr{
        background: #262729;
        height: 0.6rem;
        line-height: 0.06rem;
        font-size: 0.14rem;
        color: #fff;
        text-align: center;
      }
      /deep/.el-table td{
        text-align: center;
        height: 0.6rem;
        line-height: 0.6rem;
        color: #262729;
      }
      .publish_btn{
        display: inline-block;
        width: 0.84rem;
        height: 0.3rem;
        line-height: 0.3rem;
        text-align: center;
        border: 0.01rem solid #0378FF;
        border-radius: 0.04rem;
        color: #0378FF;
        font-size: 0.14rem;
        cursor: pointer;
        margin: 0 auto;
        margin-right: 0.3rem;
      }
      .check_btn{
        display: inline-block;
        width: 0.84rem;
        height: 0.3rem;
        line-height: 0.3rem;
        text-align: center;
        border: 0.01rem solid #0378FF;
        border-radius: 0.04rem;
        color: #0378FF;
        font-size: 0.14rem;
        cursor: pointer;
        margin: 0 auto;
      }
    }
    .paging{
      margin-top: 0.3rem;
      text-align: center;
      /deep/.el-pager li{
        font-size: 0.12rem;
        height: 0.4rem;
        line-height: 0.4rem;
      }
      /deep/.el-pager li.active{
        color: #0378FF;
      }
      /deep/.el-input__inner:focus{
        border-color: #0378FF;
      }
      /deep/.el-pagination span:not([class*=suffix]){
        font-size: 0.12rem;
        height: 0.4rem;
        line-height: 0.4rem;
      }
      /deep/.el-pagination button, .el-pagination span:not([class*=suffix]){
        height: 0.4rem;
      }
      /deep/.el-pagination__editor.el-input .el-input__inner{
        font-size: 0.12rem;
        height: 0.4rem;
        line-height: 0.4rem;
      }
    }
  }
  .mask{
    position: fixed;
    width: 100%;
    top: 0;
    right: 0;
    left: 0;
    bottom: 0;
    background:rgba(0,0,0,.5);
    z-index: 999;
    .dialog{
      width: 6.8rem;
      height: 4rem;
      border-radius: 0.08rem;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      .dialog_top{
        width: 100%;
        height: 0.6rem;
        padding: 0 0.4rem;
        overflow: hidden;
        background: #262729;
        border-radius: 0.08rem 0.08rem 0 0 ;
        .dialog_top_title{
          float: left;
          font-size: 0.18rem;
          color: #fff;
          line-height: 0.6rem;
        }
        .dialog_top_right{
          float: right;
          img{
            display: block;
            width: 0.14rem;
            height: 0.14rem;
            margin-top: 0.23rem;
            cursor: pointer;
          }
        }
      }
      .dialog_con{
        background-color: #fff;
        width: 100%;
        height: 3.4rem;
        border-radius: 0 0 0.08rem 0.08rem;
        padding-top: 0.3rem;
        /deep/.el-form-item{
          margin-bottom: 0.3rem;
          height: 0.55rem;
        }
        /deep/.el-form-item__label{
          font-size: 0.16rem;
          color: #262729;
          padding: 0 0.2rem 0 0.3rem;
          text-align: left;
          line-height: 0.55rem;
        }
        /deep/.el-form-item__content{
          line-height: 0.55rem;
        }
        /deep/.el-input__icon{
          line-height: 0.55rem;
        }
        /deep/.el-input__inner{
          width: 5.16rem;
          height: 0.55rem;
          line-height: 0.55rem;
          background:rgba(243,245,248,.8);
          border-radius: 0.08rem;
          outline: none;
          font-size: 0.16rem;
          color:rgba(7,28,53,.7);
          border: none;
          &::placeholder{
            color:rgba(7,28,53,.3);
            font-size: 0.16rem;
          }
        }
        /deep/.el-select-dropdown__item{
          font-size: 0.16rem;
        }
        .sure_btn{
          cursor: pointer;
          margin: 0.3rem auto 0;
          width: 2.4rem;
          height: 0.6rem;
          line-height: 0.6rem;
          border-radius: 0.08rem;
          background: #0378FF;
          font-size: 0.18rem;
          color: #fff;
          text-align: center;
        }
      }
    }
  }
}
</style>
